package xyz.paragon.product.persistence.service.specification;

import org.springframework.data.jpa.domain.Specification;
import xyz.paragon.product.persistence.domain.Product;
import xyz.paragon.product.persistence.domain.Product_;

import java.util.Map;

public final class ProductSpecifications {

    public static Specification<Product> createProductSpecifications(Map<String, String> params) {

        if (params == null || params.isEmpty()) {
            return null;
        }

        return nameLike(params.get("name"))
                .and(descriptionLike(params.get("description")));
    }

    public static Specification<Product> nameLike(String searchString) {

        if (searchString == null || searchString.isEmpty()) {
            return null;
        }

        return (root, query, builder) -> builder.like(root.get(Product_.name), String.valueOf(searchString));
    }

    public static Specification<Product> descriptionLike(String searchString) {

        if (searchString == null || searchString.isEmpty()) {
            return null;
        }

        return (root, query, builder) -> builder.like(root.get(Product_.description), String.valueOf(searchString));
    }
}
